import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _initializerDefineProperty from "@babel/runtime/helpers/esm/initializerDefineProperty";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _initializerWarningHelper from "@babel/runtime/helpers/esm/initializerWarningHelper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isEmpty from "lodash/isEmpty";

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _dec11, _dec12, _dec13, _dec14, _dec15, _dec16, _dec17, _class, _class2, _descriptor;

/**
 * CommonlyUsed -常用功能
 * @date: 2019-02-22
 */
import React from 'react';
import { connect } from 'dva';
import { withRouter } from 'dva/router';
import { Bind } from 'lodash-decorators';
import { Button, Modal, Tree, message, Row, Col, Card } from 'antd';
import intl from 'utils/intl';
import notification from 'utils/notification';
import { STRICT_URL } from 'utils/regExp';
import formatterCollections from 'utils/intl/formatterCollections';
import styles from "./Cards.less?modules";
var CommonlyUsed = (_dec = connect(function (_ref) {
  var commonlyUsed = _ref.commonlyUsed,
      global = _ref.global,
      loading = _ref.loading;
  return {
    global: global,
    commonlyUsed: commonlyUsed,
    // allLoading: loading.effects['commonlyUsed/queryAllFunctions'],
    addLoading: loading.effects['commonlyUsed/addFunctions']
  };
}), _dec2 = formatterCollections({
  code: 'ciam.commonlyUsed'
}), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), _dec9 = Bind(), _dec10 = Bind(), _dec11 = Bind(), _dec12 = Bind(), _dec13 = Bind(), _dec14 = Bind(), _dec15 = Bind(), _dec16 = Bind(), _dec17 = Bind(), _dec(_class = _dec2(_class = withRouter(_class = (_class2 = /*#__PURE__*/function (_React$Component) {
  _inherits(CommonlyUsed, _React$Component);

  var _super = _createSuper(CommonlyUsed);

  function CommonlyUsed(props) {
    var _this;

    _classCallCheck(this, CommonlyUsed);

    _this = _super.call(this, props);

    _initializerDefineProperty(_this, "onCheck", _descriptor, _assertThisInitialized(_this));

    _this.state = {
      drawerVisible: false,
      expandedKeys: [],
      autoExpandParent: true,
      selectedKeys: [],
      allCheckedKeys: {}
    };
    _this.addFunctionList = [];
    _this.allCheckedKeys = {};
    return _this;
  }

  _createClass(CommonlyUsed, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      this.handleSearch();
    }
    /**
     * 查询固定的常用功能
     */

  }, {
    key: "handleSearch",
    value: function handleSearch() {
      var _this$props = this.props,
          dispatch = _this$props.dispatch,
          cardParams = _this$props.cardParams;
      var params = {};

      if (cardParams) {
        this.props.cardParams.split('&').forEach(function (item) {
          var arr = item.split('=');
          var content = arr[1];
          params[arr[0]] = content;
        });
      }

      var maxNum = Number(params.maxNum) || 7;
      dispatch({
        type: 'commonlyUsed/queryFunctions',
        payload: {
          maxNum: maxNum
        }
      }).then(function (res) {
        if (res) {
          dispatch({
            type: 'commonlyUsed/updateState',
            payload: {
              commonlyUsedLoading: false,
              maxNum: maxNum
            }
          });
        }
      });
    }
    /**
     * 查询全部常用功能
     */

  }, {
    key: "handleDelete",
    value: function handleDelete(item) {
      var _this2 = this;

      var dispatch = this.props.dispatch;
      dispatch({
        type: 'commonlyUsed/deleteFunction',
        payload: {
          _token: item._token,
          menuId: item.menuId
        }
      }).then(function (res) {
        if (res) {
          _this2.handleSearch();
        }
      });
    }
    /**
     * 通过checkedKey去查找allFunction中对应的数据
     * 然后组成新的数组addFunctionList传给后端
     * @param allFunction 全部的常用功能
     * @param checkedKey  选择的常用功能
     */

  }, {
    key: "handleAddList",
    value: function handleAddList(allFunction, checkedKey) {
      for (var i = 0; i < allFunction.length; i++) {
        if ((checkedKey.indexOf(String(allFunction[i].id)) !== -1 || checkedKey.indexOf(allFunction[i].id) !== -1) && !allFunction[i].children) {
          this.addFunctionList.push({
            menuId: allFunction[i].id // orderSeq: 0,

          });
        } else if (allFunction[i].children) {
          this.handleAddList(allFunction[i].children, checkedKey);
        }
      }
    } // 确定添加需要显示的常用功能

  }, {
    key: "onOk",
    value: function onOk() {
      var _this3 = this;

      var _this$props2 = this.props,
          dispatch = _this$props2.dispatch,
          cardParams = _this$props2.cardParams,
          menu = _this$props2.global.menu;
      var allFunction = menu;
      var allCheckedKeys = this.state.allCheckedKeys;
      var checkedKeys = [];
      this.addFunctionList = [];
      /* eslint-disable */

      for (var i in allCheckedKeys) {
        checkedKeys.push.apply(checkedKeys, _toConsumableArray(allCheckedKeys[i]));
      }
      /* eslint-enable */


      var checkedKey = [];

      for (var _i = 0; _i < checkedKeys.length; _i += 1) {
        if (checkedKey.indexOf(checkedKeys[_i]) === -1) {
          checkedKey.push(checkedKeys[_i]);
        }
      }

      this.handleAddList(allFunction, checkedKey);

      if (!_isEmpty(this.addFunctionList)) {
        var params = {};

        if (cardParams) {
          this.props.cardParams.split('&').forEach(function (item) {
            var arr = item.split('=');
            var content = arr[1];
            params[arr[0]] = content;
          });
        }

        var maxNum = Number(params.maxNum) || 7;

        if (this.addFunctionList.length > maxNum) {
          message.warning(intl.get("ciam.commonlyUsed.view.commonlyUsed.warning.001message", {
            num: maxNum
          }).d("\u9009\u62E9\u7684\u5E38\u7528\u529F\u80FD\u4E0D\u80FD\u8D85\u8FC7".concat(maxNum, "\u4E2A\uFF01")));
        } else {
          dispatch({
            type: 'commonlyUsed/addFunctions',
            payload: this.addFunctionList
          }).then(function (res) {
            if (res) {
              dispatch({
                type: 'commonlyUsed/updateState',
                payload: {
                  commonlyUsedLoading: true
                }
              });
              _this3.addFunctionList = [];

              _this3.handleSearch();

              notification.success();

              _this3.hideModal();
            }
          });
        }
      } else {
        message.warning(intl.get("ciam.commonlyUsed.view.commonlyUsed.warning.002message").d('请选择常用功能！'));
      }
    } // 点击常用功能实现跳转

  }, {
    key: "handleJump",
    value: function handleJump(menuRoute) {
      var history = this.props.history;

      if (STRICT_URL.test(menuRoute)) {
        var targetWin = window.open(menuRoute);
        targetWin.opener = null;
        return;
      }

      history.push("".concat(menuRoute));
    }
  }, {
    key: "onExpand",
    value: function onExpand(expandedKeys) {
      this.setState({
        expandedKeys: expandedKeys,
        autoExpandParent: false
      });
    }
  }, {
    key: "onSelect",
    value: function onSelect(selectedKeys) {
      this.setState({
        selectedKeys: selectedKeys
      });
    }
  }, {
    key: "renderTreeNodes",
    value: function renderTreeNodes(data) {
      var _this4 = this;

      return data.map(function (item) {
        if (item.children) {
          return /*#__PURE__*/React.createElement(Tree.TreeNode, {
            title: item.title,
            key: item.key,
            dataRef: item
          }, _this4.renderTreeNodes(item.children));
        }

        return /*#__PURE__*/React.createElement(Tree.TreeNode, item);
      });
    }
    /**
     * 给全部常用功能加key
     */

  }, {
    key: "handleAddKey",
    value: function handleAddKey(data) {
      for (var i = 0; i < data.length; i += 1) {
        /* eslint-disable */
        data[i] = _objectSpread({
          key: data[i].id
        }, data[i]);
        /* eslint-enable */

        if (data[i].children) {
          this.handleAddKey(data[i].children);
        }
      }

      return data;
    }
    /**
     * 给allCheckedKeys赋初始值
     */

  }, {
    key: "handleCheckedKey",
    value: function handleCheckedKey(index, data, nowSearchKey) {
      if (data.key === nowSearchKey) {
        this.allCheckedKeys[index] = [];
        this.allCheckedKeys[index].push(nowSearchKey);
        this.setState({
          allCheckedKeys: this.allCheckedKeys
        });
        return true;
      }

      if (data.children) {
        this.handleCheckedList(index, data.children, nowSearchKey);
      }
    }
  }, {
    key: "handleCheckedList",
    value: function handleCheckedList(index, data, nowSearchKey) {
      for (var i = 0; i < data.length; i += 1) {
        if (data[i].key === nowSearchKey) {
          if (!this.allCheckedKeys[index]) {
            this.allCheckedKeys[index] = [];
          }

          this.allCheckedKeys[index].push(nowSearchKey);
          this.setState({
            allCheckedKeys: this.allCheckedKeys
          });
          return true;
        } else if (data[i].children) {
          this.handleCheckedLists(index, data[i].children, nowSearchKey);
        }
      }
    }
  }, {
    key: "handleCheckedLists",
    value: function handleCheckedLists(index, data, nowSearchKey) {
      for (var i = 0; i < data.length; i += 1) {
        if (data[i].key === nowSearchKey) {
          if (!this.allCheckedKeys[index]) {
            this.allCheckedKeys[index] = [];
          }

          this.allCheckedKeys[index].push(nowSearchKey);
          this.setState({
            allCheckedKeys: this.allCheckedKeys
          });
          return true;
        }
      }
    } // 打开Modal框

  }, {
    key: "openModal",
    value: function openModal() {
      // this.handleAllSearch();
      var dispatch = this.props.dispatch;
      dispatch({
        type: 'commonlyUsed/updateState',
        payload: {
          isListLoad: false
        }
      });
      this.setState({
        drawerVisible: true
      });
    } // 关闭Modal框

  }, {
    key: "hideModal",
    value: function hideModal() {
      this.setState({
        allCheckedKeys: {},
        drawerVisible: false
      });
    }
    /**
     * 只有请求model数据成功后才给allCheckedKeys重新赋初始值
     */

  }, {
    key: "handleIsListLoad",
    value: function handleIsListLoad() {
      var dispatch = this.props.dispatch;
      dispatch({
        type: 'commonlyUsed/updateState',
        payload: {
          isListLoad: true
        }
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this5 = this;

      var _this$state = this.state,
          drawerVisible = _this$state.drawerVisible,
          allCheckedKeys = _this$state.allCheckedKeys;
      var _this$props3 = this.props,
          addLoading = _this$props3.addLoading,
          _this$props3$commonly = _this$props3.commonlyUsed;
      _this$props3$commonly = _this$props3$commonly === void 0 ? {} : _this$props3$commonly;
      var commonlyUsedLoading = _this$props3$commonly.commonlyUsedLoading,
          _this$props3$commonly2 = _this$props3$commonly.functions,
          functions = _this$props3$commonly2 === void 0 ? [] : _this$props3$commonly2,
          _this$props3$commonly3 = _this$props3$commonly.checkedKeys,
          checkedKeys = _this$props3$commonly3 === void 0 ? [] : _this$props3$commonly3,
          isListLoad = _this$props3$commonly.isListLoad,
          menu = _this$props3.global.menu;
      var allFunction = menu;
      var allList = [];

      if (!_isEmpty(allFunction)) {
        allList = this.handleAddKey(allFunction);

        if (!isListLoad) {
          for (var i = 0; i < checkedKeys.length; i += 1) {
            for (var j = 0; j < allList.length; j += 1) {
              this.handleCheckedKey(j, allList[j], checkedKeys[i]);
            }
          }

          this.handleIsListLoad();
        }
      }

      return /*#__PURE__*/React.createElement(Row, null, /*#__PURE__*/React.createElement(Row, {
        className: styles.commonlyUsed
      }, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("span", {
        className: styles['card-title']
      }, intl.get("ciam.commonlyUsed.view.message.commonUseFunc").d('常用功能')), /*#__PURE__*/React.createElement("span", {
        className: styles['set-up'],
        onClick: this.openModal
      }, intl.get("ciam.commonlyUsed.view.message.set").d('设置'))), commonlyUsedLoading === true ? /*#__PURE__*/React.createElement(Card, {
        loading: commonlyUsedLoading,
        bordered: false,
        bodyStyle: {
          padding: '0 20px'
        }
      }) : /*#__PURE__*/React.createElement(Col, null, functions && functions.map(function (item) {
        return /*#__PURE__*/React.createElement(Button, {
          key: "commonly-item-".concat(item.menu),
          className: styles['content-button'],
          onClick: function onClick() {
            return _this5.handleJump(item.menuRoute);
          }
        }, item.title);
      }))), /*#__PURE__*/React.createElement(Modal, {
        title: intl.get("ciam.commonlyUsed.model.commonlyUsed.modalTitle").d('固定至常用功能'),
        visible: drawerVisible,
        onOk: this.onOk,
        onCancel: this.hideModal,
        confirmLoading: addLoading,
        width: "900px",
        height: "400px",
        okText: intl.get('cpaas.common.button.ok').d('确定'),
        cancelText: intl.get('cpaas.common.button.cancel').d('取消')
      }, /*#__PURE__*/React.createElement(Row, null, allList.map(function (item, index) {
        return /*#__PURE__*/React.createElement(Col, {
          span: 8,
          key: item.id
        }, /*#__PURE__*/React.createElement(Tree, {
          checkable: true,
          onExpand: _this5.onExpand,
          expandedKeys: _this5.state.expandedKeys,
          autoExpandParent: _this5.state.autoExpandParent,
          onCheck: _this5.onCheck(index),
          checkedKeys: allCheckedKeys[index],
          onSelect: _this5.onSelect,
          selectedKeys: _this5.state.selectedKeys
        }, _this5.renderTreeNodes([item])));
      }))));
    }
  }]);

  return CommonlyUsed;
}(React.Component), (_applyDecoratedDescriptor(_class2.prototype, "handleSearch", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSearch"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleDelete", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "handleDelete"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleAddList", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "handleAddList"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "onOk", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "onOk"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleJump", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "handleJump"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "onExpand", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "onExpand"), _class2.prototype), _descriptor = _applyDecoratedDescriptor(_class2.prototype, "onCheck", [_dec9], {
  configurable: true,
  enumerable: true,
  writable: true,
  initializer: function initializer() {
    var _this6 = this;

    return function (index) {
      return function (checkedKeys) {
        var allCheckedKeys = _this6.state.allCheckedKeys;
        allCheckedKeys[index] = checkedKeys;

        _this6.setState(allCheckedKeys);
      };
    };
  }
}), _applyDecoratedDescriptor(_class2.prototype, "onSelect", [_dec10], Object.getOwnPropertyDescriptor(_class2.prototype, "onSelect"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "renderTreeNodes", [_dec11], Object.getOwnPropertyDescriptor(_class2.prototype, "renderTreeNodes"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleAddKey", [_dec12], Object.getOwnPropertyDescriptor(_class2.prototype, "handleAddKey"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleCheckedKey", [_dec13], Object.getOwnPropertyDescriptor(_class2.prototype, "handleCheckedKey"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleCheckedList", [_dec14], Object.getOwnPropertyDescriptor(_class2.prototype, "handleCheckedList"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleCheckedLists", [_dec15], Object.getOwnPropertyDescriptor(_class2.prototype, "handleCheckedLists"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "openModal", [_dec16], Object.getOwnPropertyDescriptor(_class2.prototype, "openModal"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "hideModal", [_dec17], Object.getOwnPropertyDescriptor(_class2.prototype, "hideModal"), _class2.prototype)), _class2)) || _class) || _class) || _class);
export { CommonlyUsed as default };